package
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.events.*;
	import flash.geom.Point;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.ByteArray;
	import org.ascollada.core.DaeAccessor;
		
	import org.ascollada.utils.*;
	
	import org.papervision3d.Papervision3D;
	import org.papervision3d.cameras.*;
	import org.papervision3d.core.*;
	import org.papervision3d.core.proto.*;
	import org.papervision3d.events.*;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.*;
	import org.papervision3d.scenes.*;
	
	public class Main extends flash.display.Sprite
	{
		/** sprite to render to */
		public var container:Sprite;
		
		/** pv3d scene */
		public var scene:Scene3D;
		
		/** pv3d camera */
		public var camera:FreeCamera3D;
		
		/** spline example */
		public var spline:DAE;
		
		[Embed (source="../../../../../../trunk/meshes/pv3d-spline.dae", mimeType="application/octet-stream")]
		public var splineClass:Class;
		
		/**
		 * 
		 * @return
		 */
		public function Main():void
		{
			init();
		}
		
		/**
		 * 
		 * @return
		 */
		private function init():void
		{
			this.stage.quality = StageQuality.LOW;
			
			//Papervision3D.VERBOSE = false;
			
			MaterialObject3D.DEFAULT_COLOR = 0xcccccc;
			
			_status = new TextField();
			addChild(_status);
			_status.x = _status.y = 5;
			_status.width = 300;
			_status.height = 100;
			_status.selectable = false;
			_status.multiline = true;
			_status.defaultTextFormat = new TextFormat( "Arial", 11, 0xffff00 );
			
			container = new Sprite();
			addChild(container);
			container.x = 400;
			container.y = 300;

			scene = new Scene3D(container);
			
			camera = new FreeCamera3D(3, 100);
			camera.z = -100;
			
			spline = new DAE( new splineClass() );
			
			scene.addChild(spline);
			
			_lastMouse = new Point();
			
			stage.addEventListener( KeyboardEvent.KEY_UP, keyUpHandler );
			stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
			stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseMoveHandler );
			
			addEventListener( Event.ENTER_FRAME, loop3D );
		}
		
		/**
		 * 
		 * @param	event
		 * @return
		 */
		private function loop3D( event:Event = null ):void
		{
			spline.rotationY--;
			
			scene.renderCamera(camera);
		}
		
		/**
		 * 
		 * @param	event
		 * @return
		 */
		private function keyUpHandler(event:KeyboardEvent):void
		{
			var keyCode:int = event.keyCode;
			switch(keyCode){
				case "W".charCodeAt():
					camera.moveForward(10);
				break;
				case "S".charCodeAt():
					camera.moveBackward(10);
				break;
				case "A".charCodeAt():
					camera.yaw(-1);
				break;
				case "D".charCodeAt():
					camera.yaw(1);
				break;
			}
		}
		
		/**
		 * 
		 * @param	event
		 * @return
		 */
		private function mouseDownHandler( event:MouseEvent ):void
		{
			_lastMouse.x = event.stageX;
			_lastMouse.y = event.stageY;
			_orbiting = true;
		}
		
		/**
		 * 
		 * @param	event
		 * @return
		 */
		private function mouseUpHandler( event:MouseEvent ):void
		{
			_orbiting = false;
		}
		
		/**
		 * 
		 * @param	event
		 * @return
		 */
		private function mouseMoveHandler( event:MouseEvent ):void
		{
			var dx:Number = event.stageX - _lastMouse.x;
			var dy:Number = event.stageY - _lastMouse.y;
			
			if( _orbiting && this.spline )
			{
				this.spline.rotationY += dx;
				
				_lastMouse.x = event.stageX;
				_lastMouse.y = event.stageY;
			}
		}
		
		private var _status:TextField;

		private var _orbiting:Boolean = false;
		
		private var _lastMouse:Point;
	}
}
